package com.zhiwei.msf.app.service.impl;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zhiwei.msf.app.dto.DictionaryDTO;
import com.zhiwei.msf.app.dto.DictionaryQueryDto;
import com.zhiwei.msf.app.mapper.DictionaryMapper;
import com.zhiwei.msf.app.mapper.ExtDictionaryMapper;
import com.zhiwei.msf.app.po.Dictionary;
import com.zhiwei.msf.app.po.DictionaryExample;
import com.zhiwei.msf.app.service.DictionaryService;
import com.zhiwei.msf.common.constant.DatabaseConstants;
import com.zhiwei.msf.common.dto.PageDataDto;
import com.zhiwei.msf.common.util.BeanConverterUtil;
import com.zhiwei.msf.common.util.SqlUtil;
import com.zhiwei.msf.common.util.UuidUtil;

import lombok.extern.slf4j.Slf4j;

/**
 * @author ZHIWEI.YANG
 * @createtime 2019/11/10 - 16:36
 * @decription 数据字典服务
 */
@Service
@Slf4j
public class DictionaryServiceImpl implements DictionaryService {

    @Autowired
    private DictionaryMapper dictionaryMapper;
    @Autowired
    private ExtDictionaryMapper extDictionaryMapper;

    @Override
    public List<DictionaryDTO> queryPublishDictByTypeCode(String parentType) {
        DictionaryExample dictionaryExample = new DictionaryExample();
        DictionaryExample.Criteria criteria = dictionaryExample.createCriteria();
        criteria.andEnableFlagEqualTo(DatabaseConstants.ENABLE);
        criteria.andParentTypeIn(Arrays.asList(parentType.split(",")));
        List<Dictionary> dictionaryList = dictionaryMapper.selectByExample(dictionaryExample);
        log.info("字典类型编码[{}]查询字典类型信息 dictionaryList:{}", parentType, dictionaryList);
        return BeanConverterUtil.convertToBeanList(dictionaryList, DictionaryDTO.class);
    }

    @Override
    public PageDataDto<DictionaryDTO> queryPublishDictByPage(DictionaryQueryDto dictionaryQueryDto) {
        DictionaryExample dictionaryExample = new DictionaryExample();
        DictionaryExample.Criteria criteria = dictionaryExample.createCriteria();
        if (StringUtils.isNotEmpty(dictionaryQueryDto.getCode())) {
            criteria.andCodeLike(SqlUtil.getLikeSplit(dictionaryQueryDto.getCode()));
        }
        if (StringUtils.isNotEmpty(dictionaryQueryDto.getValue())) {
            criteria.andValueLike(SqlUtil.getLikeSplit(dictionaryQueryDto.getValue()));
        }
        criteria.andEnableFlagEqualTo(null == dictionaryQueryDto.getEnableFlag() ? DatabaseConstants.ENABLE : dictionaryQueryDto.getEnableFlag());
        if (StringUtils.isNotEmpty(dictionaryQueryDto.getDescription())) {
            criteria.andDescriptionLike(SqlUtil.getLikeSplit(dictionaryQueryDto.getDescription()));
        }

        PageDataDto<DictionaryDTO> pageDataDto = new PageDataDto<>();
        int count = dictionaryMapper.countByExample(dictionaryExample);
        if (0 == count) {
            pageDataDto.setPageNow(dictionaryQueryDto.getPageNow());
            pageDataDto.setPageCount(0);
            pageDataDto.setRowCount(0);
            pageDataDto.setData(Collections.emptyList());
            log.info("分页请求[{}]查询字典类型信息为空", dictionaryQueryDto);
            return pageDataDto;
        }

        RowBounds rowBounds = new RowBounds((dictionaryQueryDto.getPageNow() - 1) * dictionaryQueryDto.getPageSize(),
                dictionaryQueryDto.getPageSize());
        List<Dictionary> dictionaryList = extDictionaryMapper.selectByExampleByPage(dictionaryExample, rowBounds);

        pageDataDto.setPageNow(dictionaryQueryDto.getPageNow());
        pageDataDto.setRowCount(count);
        pageDataDto.setPageCount(count % dictionaryQueryDto.getPageSize() == 0 ?
                count / dictionaryQueryDto.getPageSize() :
                count / dictionaryQueryDto.getPageSize() + 1);
        pageDataDto.setData(BeanConverterUtil.convertToBeanList(dictionaryList, DictionaryDTO.class));
        log.info("分页请求[{}]查询字典类型信息 pageDataDto:{}", dictionaryQueryDto, pageDataDto);
        return pageDataDto;
    }

    @Override
    public boolean addDictionary(DictionaryDTO dictionaryDto) {
        if (StringUtils.isEmpty(dictionaryDto.getCode()) || StringUtils.isEmpty(dictionaryDto.getValue())) {
            return false;
        }
        Dictionary dictionary = BeanConverterUtil.convertToBean(dictionaryDto, Dictionary.class);
        Date nowDate = new Date();

        //TODO 后续mybatis插件自动填充
        dictionary.setUuid(UuidUtil.getUUID());
        dictionary.setCreateTime(nowDate);
        dictionary.setUpdateTime(nowDate);
        dictionary.setCreateUser("admin");
        dictionary.setUpdateUser("admin");
        return dictionaryMapper.insert(dictionary) == 1;
    }

    @Override
    public boolean updateDictionary(DictionaryDTO dictionaryDto) {
        if (StringUtils.isEmpty(dictionaryDto.getUuid())) {
            return false;
        }
        Dictionary dictionary = BeanConverterUtil.convertToBean(dictionaryDto, Dictionary.class);
        return dictionaryMapper.updateByPrimaryKeySelective(dictionary) == 1;
    }
}